package com.supply.dl.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.supply.dl.R;
import com.supply.dl.app.ApiService;
import com.supply.dl.app.AppConfig;
import com.supply.dl.app.adapter.NormalAdapter;
import com.supply.dl.app.adapter.NormalPositionAdapter;
import com.supply.dl.app.adapter.ViewHolder;
import com.supply.dl.app.http.BaseInfo;
import com.supply.dl.app.http.BaseObserver;
import com.supply.dl.app.http.HttpMethod;
import com.supply.dl.app.http.RxSchedulers;
import com.supply.dl.bean.MaterielPositionInfoBean;
import com.supply.dl.bean.MaterierPositionPost;
import com.supply.dl.bean.PositionDetailBean;
import com.supply.dl.bean.PositionInfoBean;
import com.supply.dl.bean.RecommendPositionInfoBean;
import com.supply.dl.bean.SearchStockBean;
import com.supply.dl.utils.BigDecimalUtil;
import com.supply.dl.utils.DialogUtils;
import com.supply.dl.utils.StringUtils;
import com.supply.dl.utils.Utils;
import com.supply.dl.utils.utils.ToastUtil;
import com.supply.dl.view.MaxRecyclerView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 领料定位详情
 * <p>
 * Author:  tian <br/>
 * Time:  2018/5/8 1:26
 */
public class MaterialLocationDetailActivity extends BaseActivity {

    /**
     * 通知单id
     */
    private String noticeOrderId;

    /**
     * 物料列表
     */
    @BindView(R.id.materail_rc)
    MaxRecyclerView materail_rc;

    /**
     * 物料横向列表 适配器
     */
    private NormalAdapter<MaterielPositionInfoBean> materielAdapter;

    /**
     * 物料列表数据，已定位物料数据，未来定位物料数据
     */
    private List<MaterielPositionInfoBean> itemList, hasPostionList, notPostionList;

    /**
     * 详情数据
     */
    private PositionDetailBean mPositionDetailBean;

    /**
     * 为定位
     */
    @BindView(R.id.un_location_tv)
    TextView notLocatedTv;

    @BindView(R.id.unlocation_line)
    View notLocatedLineView;

    /**
     * 已定位
     */
    @BindView(R.id.located_tv)
    TextView hadLocatedTv;

    @BindView(R.id.had_location_line)
    View hadLocatedLineView;

    /**
     * 添加新定位按钮
     */
    @BindView(R.id.add_location_tv)
    TextView addNewPositionBtn;

    /**
     * 批量定位
     */
    @BindView(R.id.location_lot_tv)
    TextView batchPositionBtn;

    /**
     * 定位列表
     */
    @BindView(R.id.location_rc)
    MaxRecyclerView positionRecyclerView;

    /**
     * 通知单号
     */
    @BindView(R.id.common_letter_of_notice_info_showNoticeLetterNo)
    TextView showNoticeLetterNo;

    /**
     * 定位状态
     */
    @BindView(R.id.common_letter_of_notice_info_positionStatusName)
    TextView positionStatusName;

    /**
     * 业务类型
     */
    @BindView(R.id.common_letter_of_notice_info_showFirstValue)
    TextView showFirstValue;

    /**
     * 供应商信息
     */
    @BindView(R.id.common_letter_of_notice_info_showSecondValue)
    TextView showSecondValue;

    /**
     * 车牌号
     */
    @BindView(R.id.common_letter_of_notice_info_showThirdValue)
    TextView showThirdValue;

    /**
     * 司机名字
     */
    @BindView(R.id.common_letter_of_notice_info_showFourthValue)
    TextView showFourthValue;

    /**
     * 物料编码
     */
    @BindView(R.id.common_materiel_position_info_showFirstValue)
    TextView showMaterielFirstValue;

    /**
     * 物料名称
     */
    @BindView(R.id.common_materiel_position_info_showSecondValue)
    TextView showMaterielSecondValue;

    /**
     * 总数
     */
    @BindView(R.id.common_materiel_position_info_showThirdValue)
    TextView showMaterielThirdValue;

    /**
     * 标记是未定位还是已定位
     */
    @BindView(R.id.common_materiel_position_info_fourthTv)
    TextView materielFourthTv;

    /**
     * 未定位数量、已定位数量
     */
    @BindView(R.id.common_materiel_position_info_showFourthValue)
    TextView showMaterielFourthValue;

    /**
     * 定位列表数据
     */
    private ArrayList<Object> postionDatas;

    /**
     * 定位列表适配器
     */
    private NormalPositionAdapter<Object> positionAdapter;

    /**
     * 标记当前 是否是显示的 未定位信息
     */
    private boolean hasPositionShow;

    /**
     * 当前被选中的 物料对象
     */
    private MaterielPositionInfoBean currentShowMaterier;


    /**
     * 当前定位或者未定位 item项
     */
    private Object currentPostionItem;


    @Override
    protected int getLayoutResource() {
        return R.layout.activity_normal_detail_main;
    }

    @Override
    protected void initView() {
        super.initView();
        showLeftBack();
        setTitleShow("领料定位详情");

        setRightHomeClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.showToast(MaterialLocationDetailActivity.this, "home");
            }
        });

        setRightScanClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.showToast(MaterialLocationDetailActivity.this, "scan");
                Utils.startScanActivity(MaterialLocationDetailActivity.this);
            }
        });

        noticeOrderId = getIntent().getStringExtra("noticeOrderId");
        getMaterierPositionDetail();
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);

        itemList = new ArrayList<>();
        hasPostionList = new ArrayList<>();
        notPostionList = new ArrayList<>();
        postionDatas = new ArrayList<>();
    }

    @Override
    protected void initRecyclerView() {

        //初始化物料列表
        materielAdapter = new NormalAdapter<MaterielPositionInfoBean>(this, itemList) {
            @Override
            protected void convert(ViewHolder holder, MaterielPositionInfoBean data) {
                TextView name_tv = holder.getView(R.id.material_position_info_item_textView);
                if (data.isSelected) {
                    name_tv.setBackgroundResource(R.drawable.shape_search_btn_blue_bg);
                    name_tv.setTextColor(getResources().getColor(R.color.whiteColor));
                }
                //
                else {
                    name_tv.setBackgroundResource(R.drawable.shape_solid_gray_bg);
                    name_tv.setTextColor(getResources().getColor(R.color.textColor));
                }
                name_tv.setText(data.skuName);
            }

            @Override
            protected int getItemLayoutId() {
                return R.layout.materiel_position_info_item;
            }
        };

        //点击事件
        materielAdapter.setOnItemClickListener(new NormalAdapter.OnItemClickListener<MaterielPositionInfoBean>() {
            @Override
            public void onItemClick(ViewHolder viewHolder, MaterielPositionInfoBean data, int position) {

                for (MaterielPositionInfoBean item : itemList) {
                    if (!item.skuId.equals(data.skuId))
                        item.isSelected = false;
                }

                materielAdapter.notifyDataSetChanged();
                fillMaterialView();
            }
        });

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        materail_rc.setLayoutManager(linearLayoutManager);
        materail_rc.setAdapter(materielAdapter);

        //初始化
        positionAdapter = new NormalPositionAdapter<Object>(this, postionDatas) {
            @Override
            protected void convert(ViewHolder holder, final Object data, int index) {

                final TextView sure_location_tv = holder.getView(R.id.sure_location_tv);

                holder.setText(R.id.material_name_tv, "物料" + (index + 1));

                //展示未定位信息中的 推荐定位信息
                if (data instanceof RecommendPositionInfoBean) {

                    RecommendPositionInfoBean rb = (RecommendPositionInfoBean) data;
                    holder.setText(R.id.libraray_nub_tv, rb.recommendStockName);
                    holder.setText(R.id.ck_name_tv, rb.recommendWhName);
                    holder.setText(R.id.count_amout_tv, rb.amount == null ? "" : (rb.amount + ""));
                    holder.setText(R.id.weight_tv, rb.weight == null ? "" : (rb.weight + ""));
                    holder.setText(R.id.volume_tv, rb.volume == null ? "" : (rb.volume + ""));

                    holder.setText(R.id.kc_status_tv, rb.stockStatusName);
                    holder.setText(R.id.guige_tv, rb.unitName);
                    holder.setText(R.id.pici_tv, rb.batchNo);


                    if (rb.isPostPositon) {
                        sure_location_tv.setText("已提交定位信息");
                    }
                    //
                    else {
                        sure_location_tv.setText("确定定位");
                    }
                }
                //展示已定位信息中的 已定位信息
                else {
                    PositionInfoBean pb = (PositionInfoBean) data;
                    holder.setText(R.id.libraray_nub_tv, pb.stockName);
                    holder.setText(R.id.ck_name_tv, pb.whName);
                    holder.setText(R.id.count_amout_tv, pb.amount == null ? "" : (pb.amount + ""));
                    holder.setText(R.id.weight_tv, pb.weight == null ? "" : (pb.weight + ""));
                    holder.setText(R.id.volume_tv, pb.volume == null ? "" : (pb.volume + ""));
                    holder.setText(R.id.kc_status_tv, pb.stockStatusName);
                    holder.setText(R.id.guige_tv, pb.unitName);
                    holder.setText(R.id.pici_tv, pb.batchNo);

                    if (pb.isPostPositon) {
                        sure_location_tv.setText("已提交定位信息");
                    }
                    //
                    else {
                        sure_location_tv.setText("确定定位");
                    }
                }

                ((EditText) holder.getView(R.id.count_amout_tv)).addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                        if (!StringUtils.isCanUse(s.toString()))
                            return;

                        if (data instanceof RecommendPositionInfoBean) {
                            RecommendPositionInfoBean rib = (RecommendPositionInfoBean) data;
                            rib.amount = BigDecimalUtil.getBigDecimalByString(s.toString());
                        }
                        //
                        else {
                            PositionInfoBean pib = (PositionInfoBean) data;
                            pib.amount = BigDecimalUtil.getBigDecimalByString(s.toString());
                        }
                    }
                });

                ((EditText) holder.getView(R.id.weight_tv)).addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                        if (!StringUtils.isCanUse(s.toString()))
                            return;

                        if (data instanceof RecommendPositionInfoBean) {
                            RecommendPositionInfoBean rib = (RecommendPositionInfoBean) data;
                            rib.weight = BigDecimalUtil.getBigDecimalByString(s.toString());
                        }
                        //
                        else {
                            PositionInfoBean pib = (PositionInfoBean) data;
                            pib.weight = BigDecimalUtil.getBigDecimalByString(s.toString());
                        }
                    }
                });

                ((EditText) holder.getView(R.id.volume_tv)).addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                        if (!StringUtils.isCanUse(s.toString()))
                            return;

                        if (data instanceof RecommendPositionInfoBean) {
                            RecommendPositionInfoBean rib = (RecommendPositionInfoBean) data;
                            rib.volume = BigDecimalUtil.getBigDecimalByString(s.toString());
                        }
                        //
                        else {
                            PositionInfoBean pib = (PositionInfoBean) data;
                            pib.volume = BigDecimalUtil.getBigDecimalByString(s.toString());
                        }
                    }
                });

                holder.getView(R.id.libraray_nub_tv).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if ("已提交定位信息".equals(sure_location_tv.getText().toString())) {
                            return;
                        }

                        Bundle bundle = new Bundle();
                        //物料编码
                        bundle.putString("skuId", currentShowMaterier.skuCode);
                        //页面名字
                        bundle.putString("titleName", "库位选择");
                        //仓库id
                        bundle.putString("whId", "");

                        Intent it = new Intent(MaterialLocationDetailActivity.this, DepotSelectActivity.class);
                        it.putExtras(bundle);
                        startActivityForResult(it, AppConfig.BaseDataAction.REQCODE_CHOOSE_STOCK);

                        currentPostionItem = data;
                    }
                });

                holder.getView(R.id.delete_tv).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogUtils.showDialog(MaterialLocationDetailActivity.this, "提示", "确定删除当前物料吗?", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                currentPostionItem = data;

                                postionDatas.remove(data);
                                positionAdapter.notifyDataSetChanged();

                                deletePostion();
                            }
                        }).show();
                    }
                });


                sure_location_tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        currentPostionItem = data;
                        sureLocation();
                    }
                });

            }

            @Override
            protected int getItemLayoutId() {
                return R.layout.item_location;
            }
        };

        positionRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        positionRecyclerView.setAdapter(positionAdapter);

        isHadLocation(false);
    }

    @OnClick({R.id.un_location_tv, R.id.located_tv, R.id.add_location_tv, R.id.location_lot_tv})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.un_location_tv://未定位
                isHadLocation(false);
                break;
            case R.id.located_tv://已定位
                isHadLocation(true);
                break;
            case R.id.add_location_tv://添加新定位

                if (currentShowMaterier == null) {
                    Toast.makeText(this, "没有物料不能进行添加新定位操作", Toast.LENGTH_SHORT).show();
                    return;
                }

                currentPostionItem = null;

                Bundle bundle = new Bundle();
                //物料编码
                bundle.putString("skuId", currentShowMaterier.skuCode);
                //页面名字
                bundle.putString("titleName", "库位选择");
                //仓库id
                bundle.putString("whId", "");

                Intent it = new Intent(MaterialLocationDetailActivity.this, DepotSelectActivity.class);
                it.putExtras(bundle);
                startActivityForResult(it, AppConfig.BaseDataAction.REQCODE_CHOOSE_STOCK);

                break;
        }
    }

    /**
     * 是否定位
     *
     * @param isLocation
     */
    private void isHadLocation(boolean isLocation) {

        hasPositionShow = isLocation;

        if (!isLocation) {
            notLocatedLineView.setVisibility(View.VISIBLE);
            hadLocatedLineView.setVisibility(View.INVISIBLE);
            batchPositionBtn.setVisibility(View.GONE);
            notLocatedTv.setTextColor(getResources().getColor(R.color.blue));
            hadLocatedTv.setTextColor(getResources().getColor(R.color.color_gray));

            //显示的数据进行切换
            itemList.clear();
            itemList.addAll(notPostionList);
            materielAdapter.notifyDataSetChanged();
        }
        //
        else {
            hadLocatedLineView.setVisibility(View.VISIBLE);
            notLocatedLineView.setVisibility(View.INVISIBLE);
            batchPositionBtn.setVisibility(View.GONE);
            hadLocatedTv.setTextColor(getResources().getColor(R.color.blue));
            notLocatedTv.setTextColor(getResources().getColor(R.color.color_gray));

            itemList.clear();
            itemList.addAll(hasPostionList);
            materielAdapter.notifyDataSetChanged();
        }

        fillMaterialView();
    }

    /**
     * 获取领料定库详细数据
     */
    private void getMaterierPositionDetail() {
        ApiService api = HttpMethod.getInstance().create(ApiService.class);
        api.getMaterierPositionDetail(noticeOrderId)
                .compose(RxSchedulers.<BaseInfo<PositionDetailBean>>compose(this))
                .subscribe(new BaseObserver<PositionDetailBean>() {
                    @Override
                    public void onSuccess(BaseInfo<PositionDetailBean> baseInfo) {
                        if (baseInfo != null && baseInfo.getData() != null) {
                            mPositionDetailBean = baseInfo.getData();

                            //填充顶部
                            fillTopView();

                            //横向物料布局的填充
                            fillHorizontalRecyclerView();

                            fillMaterialView();
                        }
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    /**
     * 填充订单布局信息
     */
    private void fillTopView() {
        //通知单订单号
        showNoticeLetterNo.setText(mPositionDetailBean.noticeOrderNo);
        //定库状态
        positionStatusName.setText(mPositionDetailBean.positionStatusName);
        //业务类型
        showFirstValue.setText(mPositionDetailBean.bussTypeName);
        //供应商
        showSecondValue.setText(mPositionDetailBean.supplierName);
        //车牌号
        showThirdValue.setText(mPositionDetailBean.carNo);
        //司机
        showFourthValue.setText(mPositionDetailBean.driverName);
    }

    /**
     * 填充横向 物料布局,注意这里要区分是，已定位还是未来定位的数据
     */
    private void fillHorizontalRecyclerView() {
        if (mPositionDetailBean.getHasPositionInfo() == null && mPositionDetailBean.getNotPositionInfo() == null)
            return;

        /*设置已定位和未定位，第一项为选中*/

        if (mPositionDetailBean.getHasPositionInfo() != null && mPositionDetailBean.getHasPositionInfo().size() > 0) {
            hasPostionList.addAll(mPositionDetailBean.getHasPositionInfo());
            hasPostionList.get(0).isSelected = true;

            //设定当前数据为已定位
            for (MaterielPositionInfoBean mpib : mPositionDetailBean.getHasPositionInfo()) {
                mpib.hadPostion = true;
            }
        }

        if (mPositionDetailBean.getNotPositionInfo() != null && mPositionDetailBean.getNotPositionInfo().size() > 0) {
            notPostionList.addAll(mPositionDetailBean.getNotPositionInfo());
            notPostionList.get(0).isSelected = true;

            //设定当前数据为未定位
            for (MaterielPositionInfoBean mpib : mPositionDetailBean.getNotPositionInfo()) {
                mpib.hadPostion = false;
            }
        }

        //注意初始情况下，是先显示未定位物料
        itemList.addAll(notPostionList);
        materielAdapter.notifyDataSetChanged();
    }

    /**
     * 填充物料显示布局
     */
    private void fillMaterialView() {

        if (itemList.size() == 0) {
            showMaterielFirstValue.setText("");
            showMaterielSecondValue.setText("");
            showMaterielThirdValue.setText("");
            showMaterielFourthValue.setText("");
            materielFourthTv.setText(hasPositionShow ? "已定位数量:" : "未定位数据");

            currentShowMaterier = null;


            postionDatas.clear();
            positionAdapter.notifyDataSetChanged();

            return;
        }

        for (MaterielPositionInfoBean item : itemList) {
            if (item.isSelected) {
                currentShowMaterier = item;
                break;
            }
        }

        showMaterielFirstValue.setText(currentShowMaterier.skuCode);
        showMaterielSecondValue.setText(currentShowMaterier.skuName);
        showMaterielThirdValue.setText(currentShowMaterier.detailAmount + "件/" + currentShowMaterier.detailWeight + "吨");
        showMaterielFourthValue.setText(currentShowMaterier.notPositionAmount + "件/" + currentShowMaterier.notPositionWeight + "吨");
        materielFourthTv.setText(hasPositionShow ? "已定位数量:" : "未定位数量:");


        postionDatas.clear();
        postionDatas.addAll(currentShowMaterier.hadPostion ? currentShowMaterier.getPositionInfoList() :
                currentShowMaterier.getRecommendPositionInfoList());
        positionAdapter.notifyDataSetChanged();

        currentPostionItem = null;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data == null)
            return;
        switch (requestCode) {
            case AppConfig.BaseDataAction.REQCODE_CHOOSE_STOCK:
                if (data.hasExtra("selectBean")) {
                    SearchStockBean selectBean = (SearchStockBean) data.getSerializableExtra("selectBean");
                    dealSelectStockResult(selectBean);
                }
                break;
        }
    }

    /**
     * 处理选择库存
     *
     * @param selectBean
     */
    public void dealSelectStockResult(SearchStockBean selectBean) {
        /**在获取到数据过后，需要注意是 新整还是更改*/
        //如果是新整
        if (currentPostionItem == null) {

            if (!currentShowMaterier.hadPostion) {
                RecommendPositionInfoBean rcItem = new RecommendPositionInfoBean();
                rcItem.recommendStockId = selectBean.getStockId();
                rcItem.recommendStockName = selectBean.getStockName();
                rcItem.recommendWhId = selectBean.getWhId();
                rcItem.recommendWhName = selectBean.getWhName();
                rcItem.invId = selectBean.getId();
                rcItem.stockStatusName = selectBean.getStockStatusName();

                rcItem.availableInventoryPiece = new BigDecimal(selectBean.getAvailableInventoryPiece());
                rcItem.packageWeight = selectBean.getPackageWeight();
                rcItem.batchNo = selectBean.getBatchNo();
                rcItem.unitName = selectBean.getUnitName();
                rcItem.unitId = selectBean.getUnitId();

                currentShowMaterier.getRecommendPositionInfoList().add(rcItem);
                postionDatas.add(rcItem);
            }
            //
            else {
                PositionInfoBean pb = new PositionInfoBean();
                pb.stockId = selectBean.getStockId();
                pb.stockName = selectBean.getStockName();
                pb.whId = selectBean.getWhId();
                pb.whName = selectBean.getWhName();
                pb.invId = selectBean.getId();
                pb.stockStatusName = selectBean.getStockStatusName();

                pb.availableInventoryPiece =  BigDecimalUtil.getBigDecimalByString(selectBean.getAvailableInventoryPiece());
                pb.packageWeight = BigDecimalUtil.getBigDecimalByString(selectBean.getPackageWeight());
                pb.batchNo = selectBean.getBatchNo();
                pb.unitName = selectBean.getUnitName();
                pb.unitId = selectBean.getUnitId();

                currentShowMaterier.getPositionInfoList().add(pb);
                postionDatas.add(pb);
            }

            positionAdapter.notifyDataSetChanged();

            return;
        }

        /**下面是更改操作*/
        //未定位
        if (currentPostionItem instanceof RecommendPositionInfoBean) {
            RecommendPositionInfoBean rcItem = (RecommendPositionInfoBean) currentPostionItem;
            rcItem.recommendStockId = selectBean.getStockId();
            rcItem.recommendStockName = selectBean.getStockName();
            rcItem.recommendWhId = selectBean.getWhId();
            rcItem.recommendWhName = selectBean.getWhName();
            rcItem.invId = selectBean.getId();
            rcItem.stockStatusName = selectBean.getStockStatusName();

            rcItem.availableInventoryPiece = new BigDecimal(selectBean.getAvailableInventoryPiece());
            rcItem.packageWeight = selectBean.getPackageWeight();
            rcItem.batchNo = selectBean.getBatchNo();
            rcItem.unitName = selectBean.getUnitName();
            rcItem.unitId = selectBean.getUnitId();

            rcItem.isPostPositon = false;

        }
        //已定位
        else {
            PositionInfoBean rcItem = (PositionInfoBean) currentPostionItem;
            rcItem.stockId = selectBean.getStockId();
            rcItem.stockName = selectBean.getStockName();
            rcItem.whId = selectBean.getWhId();
            rcItem.whName = selectBean.getWhName();
            rcItem.invId = selectBean.getId();
            rcItem.stockStatusName = selectBean.getStockStatusName();

            rcItem.availableInventoryPiece =  BigDecimalUtil.getBigDecimalByString(selectBean.getAvailableInventoryPiece());
            rcItem.packageWeight = BigDecimalUtil.getBigDecimalByString(selectBean.getPackageWeight());
            rcItem.batchNo = selectBean.getBatchNo();
            rcItem.unitName = selectBean.getUnitName();
            rcItem.unitId = selectBean.getUnitId();

            rcItem.isPostPositon = false;
        }

        positionAdapter.notifyDataSetChanged();
    }

    /**
     * 确认提交定位
     */
    private void sureLocation() {

        MaterierPositionPost post = new MaterierPositionPost();

        //未定位
        if (!currentShowMaterier.hadPostion) {
            RecommendPositionInfoBean rpib = (RecommendPositionInfoBean) currentPostionItem;
            if (rpib.amount == null) {
                Toast.makeText(this, "请填入件数", Toast.LENGTH_SHORT).show();
                return;
            }

            post.orderId = mPositionDetailBean.orderId;
            post.orderNo = mPositionDetailBean.orderNo;
            post.orderDetailId = currentShowMaterier.orderDetailId;

            post.noticeOrderId = noticeOrderId;
            post.noticeOrderNo = mPositionDetailBean.noticeOrderNo;
            post.noticeOrderDetailId = currentShowMaterier.noticeOrderDetailId;

            //(1:出库)
            post.operateType = "1";
            post.whId = rpib.recommendWhId;
            post.stockId = rpib.recommendStockId;
            post.invId = rpib.invId;
            post.recommendStockId = rpib.recommendStockId;
            post.recommendWhId = rpib.recommendWhId;

            post.skuId = currentShowMaterier.skuId;
            post.skuCode = currentShowMaterier.skuCode;
            post.skuName = currentShowMaterier.skuName;

            post.amount = rpib.amount;
            post.weight = rpib.weight;
            post.volume = rpib.volume;

        }
        //已定位
        else {

            PositionInfoBean pb = (PositionInfoBean) currentPostionItem;

            if (pb.amount == null) {
                Toast.makeText(this, "请填入件数", Toast.LENGTH_SHORT).show();
                return;
            }

            post.id = pb.id;
            post.orderId = mPositionDetailBean.orderId;
            post.orderNo = mPositionDetailBean.orderNo;
            post.orderDetailId = currentShowMaterier.orderDetailId;

            post.noticeOrderId = noticeOrderId;
            post.noticeOrderNo = mPositionDetailBean.noticeOrderNo;
            post.noticeOrderDetailId = currentShowMaterier.noticeOrderDetailId;

            //(1:出库)
            post.operateType = "1";
            post.whId = pb.whId;
            post.stockId = pb.stockId;
            post.invId = pb.invId;

            post.skuId = currentShowMaterier.skuId;
            post.skuCode = currentShowMaterier.skuCode;
            post.skuName = currentShowMaterier.skuName;

            post.amount = pb.amount;
            post.weight = pb.weight;
            post.volume = pb.volume;
        }

        ApiService api = HttpMethod.getInstance().create(ApiService.class);
        api.getMaterierPostionSure(post)
                .compose(RxSchedulers.<BaseInfo<String>>compose(this))
                .subscribe(new BaseObserver<String>() {
                    @Override
                    public void onSuccess(BaseInfo<String> baseInfo) {
                        if (baseInfo != null && baseInfo.getData() != null) {
                            Toast.makeText(MaterialLocationDetailActivity.this, "确认定位成功!"
                                    , Toast.LENGTH_SHORT).show();

                            //未定位情况下
                            if (currentPostionItem instanceof RecommendPositionInfoBean) {
                                RecommendPositionInfoBean temp = (RecommendPositionInfoBean) currentPostionItem;
                                temp.isPostPositon = true;
                            }
                            //
                            else {
                                PositionInfoBean temp = (PositionInfoBean) currentPostionItem;
                                temp.isPostPositon = true;
                            }

                            positionAdapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        Toast.makeText(MaterialLocationDetailActivity.this, msg, Toast.LENGTH_SHORT).show();
                    }
                });
    }

    /**
     * 删除定位
     */
    private void deletePostion() {

        String ids;
        if (currentPostionItem instanceof PositionInfoBean)
            ids = ((PositionInfoBean) currentPostionItem).id;
        else
            ids = ((RecommendPositionInfoBean) currentPostionItem).id;

        ApiService api = HttpMethod.getInstance().create(ApiService.class);
        api.deleteMaterierPostion(ids)
                .compose(RxSchedulers.<BaseInfo<String>>compose(this))
                .subscribe(new BaseObserver<String>() {
                    @Override
                    public void onSuccess(BaseInfo<String> baseInfo) {

                        if (baseInfo != null && baseInfo.getData() != null) {
                            Toast.makeText(MaterialLocationDetailActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        Toast.makeText(MaterialLocationDetailActivity.this, msg, Toast.LENGTH_SHORT).show();
                    }
                });
    }

}


